package com.turing.selfwebservice.common;

import java.math.BigInteger;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.turing.post.webservice.client.CardHolderUser;
import com.turing.post.webservice.client.MerchantOper;
import com.turing.post.webservice.client.PcpsWebInput;
import com.turing.post.webservice.client.WebWscImplPortType;
import com.turing.selfwebservice.system.SessionHashTable;

public class BaseServlet extends DispatchAction {
	protected HttpSession session = null;
	protected WebWscImplPortType webWscImplPortType = null;
	protected PcpsWebInput putwebinput = new PcpsWebInput();
	protected MerchantOper merchantOper = null;
	protected BigInteger merchantOperId = null;
	protected CardHolderUser cardHolderUser = null;
	protected BigInteger cardHolderUserId = null;
	protected String cardHolderUserLogonName = null;
	protected String merchantOperUserName = null;
	protected String merchantNo = null;

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		session = request.getSession();
		SessionHashTable sht = (SessionHashTable) session
				.getAttribute("SessionHashTable");
		webWscImplPortType = sht.getWebservice();
		merchantOper = (MerchantOper) session.getAttribute("MerchantOper");
		if (null != merchantOper) {
			merchantOperId = merchantOper.getMrchOperId();
			merchantOperUserName = merchantOper.getMrchOperLogonName();
			merchantNo = merchantOper.getMerchantNo();
		}
		cardHolderUser = (CardHolderUser) session
				.getAttribute("CardHolderUser");
		if (null != cardHolderUser) {
			cardHolderUserId = cardHolderUser.getCardHolderUserId();
			cardHolderUserLogonName = cardHolderUser.getLogonName();
		}
		return super.execute(mapping, form, request, response);
	}

	protected ActionForward returnError(ActionMapping mapping,
			HttpServletRequest request) {
		request.setAttribute("message", "服务器连接失败");
		request.setAttribute("code", "404");
		return mapping.findForward("error");
	}

	/**
	 * 查询参数字符转换
	 * 
	 * @param request
	 * @param value
	 * @return
	 * @throws Exception
	 */
	protected String StringTransformation(HttpServletRequest request,
			String value) throws Exception {
		String val = request.getParameter(value);
		if (StringUtils.isBlank(val)) {
			return null;
		}
		return URLDecoder.decode(val.trim(), "UTF-8");
	}

	/**
	 * 从request的Parameter中得值
	 * 
	 * @param request
	 * @param value
	 * @return
	 * @throws Exception
	 */
	protected String getParameter(HttpServletRequest request, String value)
			throws Exception {
		String val = request.getParameter(value);
		if (StringUtils.isBlank(val)) {
			return null;
		}
		return val.trim();
	}

	/**
	 * 得到Int参数
	 * 
	 * @param request
	 * @param name
	 * @param defaultNum
	 *            默认值
	 * @return
	 */
	public static int getIntParameter(HttpServletRequest request, String name,
			int defaultNum) {
		String temp = request.getParameter(name);
		if (temp != null && !temp.equals("")) {
			int num = defaultNum;
			try {
				num = Integer.parseInt(temp);
			} catch (Exception ignored) {
			}
			return num;
		} else {
			return defaultNum;
		}
	}
}
